#!/usr/bin/env python
# -*- coding:utf-8 -*-


def operate01():
    a = "qiwsir"
    c = "qiwsir"
    b = "python"
    print a == b
    print a == c
    print not a == c


def operator02():
    name = "hello"
    name = "qiwsir" if "laoqi" else "github"  # A = Y if X else Z  如果X为真，那么就执行A=Y;如果X为假，就执行A=Z
    print name
    name = "qiwsir" if "" else "github"
    print name


def operator03():
    ls_line = ['Hello', 'I am qiwsir', 'Welcome you', 'qiwsir']
    for i in range(len(ls_line)):  # range是序列的集合
        print ls_line[i]

    for i in range(0, len(ls_line), 2):  # range是序列的集合
        print ls_line[i]

    print range(0, 9, 2)


def operator04():
    aliquot = []
    for n in range(1, 100):
        if n % 3 == 0:
            aliquot.append(n)

    print aliquot


# 所有序列型对象，都能够用for来循环
def operator05():
    name_str = "qiwsir"
    for i in name_str:  # 可以对str使用for循环
        print i
    name_list = list(name_str)  # 对list也能用
    print list
    for i in name_list:
        print i
    name_set = set(name_str)  # set还可以用
    for i in name_set:
        print i
    # 遍历dic
    name_dict = {"name": "qiwsir", "lang": "python", "website": "qiwsir.github.io"}  # dict也不例外，这里本质上是将字典的键拿出来，成为序列后进行循环
    for i in name_dict:
        print i, "-->", name_dict[i]

    for k in name_dict.keys():  # key遍历
        print k, name_dict[k]
    for v in name_dict.values():  # 效率低下，不建议使用
        print v
    for k, v in name_dict.items():  # item遍历
        print k, v
    for k, v in name_dict.iteritems():  # iterator 遍历
        print k, v
    for v in name_dict.itervalues():  # iterator 遍历
        print v


def operator06():
    c = [1, 2, 3]
    d = [9, 8, 7, 6]
    print zip(c, d)
    e = []
    for x, y in zip(c, d):  # 对应序列，位置相加（矩阵运算）
        e.append(x + y)
    print e

    s = {"name": "qiwsir"}  # zip是一个内置函数，它的参数必须是某种序列数据类型，如果是字典，那么键视为序列
    t = {"lang": "python"}
    print zip(s, t)
    result = [(2, 11), (4, 13), (6, 15), (8, 17)]
    print zip(*result)
    for (i, val) in enumerate(c):  # enumerate内置函数,得到一个list的每个元素索引，然后在用list[i]的方式得到该元素
        print str(i) + ' is ' + str(val)
    mylist = ["qiwsir", 703, "python"]  # list与enumerate
    print list(enumerate(mylist))


def operator07():
    raw = "Do you love Canglaoshi? Canglaoshi is a good teacher."
    raw_list = raw.split(" ")
    print raw_list
    for i, string in enumerate(raw_list):
        if 'Canglaoshi' in string:
            raw_list[i] = 'PHP'
    print raw_list


def operator08():
    power2 = []
    for i in range(1, 10):
        power2.append(i * i)
    squares = [x ** 2 for x in range(1, 10)]  # Python确实优雅
    print squares
    mybag = [' glass', ' apple', 'green leaf ']  # 有的前面有空格，有的后面有空格
    print mybag
    mybag1 = [one.strip() for one in mybag]  # 去掉元素前后的空格   --- 好优雅的操作
    print mybag1


if __name__ == '__main__':
    operate01()
    print "==============================="
    operator02()
    print "==============================="
    operator03()
    print "==============================="
    operator04()
    print "==============================="
    operator05()
    print "==============================="
    operator06()
    print "==============================="
    operator07()
    print "==============================="
    operator08()
